package com.wondertek.poms.core.service.impl;

import cn.hutool.core.convert.Convert;
import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.IChannelInfoPackageService;
import com.wondertek.poms.core.service.IChannelPackageService;
import com.wondertek.poms.dao.po.ChannelInfoPackage;
import com.wondertek.poms.dao.po.ChannelPackage;
import com.wondertek.poms.dao.pojo.ChannelPackagePojo;
import com.wondertek.poms.dao.repo.IChannelInfoPackageDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Optional;

@Service
public class ChannelInfoPackageServiceImpl extends ServiceImpl<ChannelInfoPackage,Long> implements IChannelInfoPackageService {

    @Autowired
    private IChannelInfoPackageDao channelInfoPackageDao;
    @Autowired
    private IChannelPackageService channelPackageService;

    @Override
    public List<ChannelInfoPackage> findAllByChannelId(String channelId) {
        return channelInfoPackageDao.findAllByChannelId(channelId);
    }

    @Override
    public List<ChannelInfoPackage> findAllByPackageId(Long packageId) {
        return channelInfoPackageDao.findAllByChannelPackageId(packageId);
    }

    @Override
    @Transactional
    public void saveChannelInfo(HashSet<String> set, Long packageId) {
        ChannelPackage channelPackage= channelPackageService.findById(packageId);
        if(channelPackage != null){
            channelPackage.setIsPublish(0);
            ChannelPackage.putTimeAndUser(channelPackage);
            channelPackageService.save(channelPackage);
            for (String id : set) {
                ChannelInfoPackage channelInfoPackage=new ChannelInfoPackage();
                channelInfoPackage.setChannelPackageId(packageId);
                channelInfoPackage.setChannelId(id);
                List<ChannelInfoPackage> list=jpaRepository.findAll(Example.of(channelInfoPackage));
                if(list != null && list.size()>0){
                    continue;
                }
                ChannelInfoPackage.putTimeAndUser(channelInfoPackage);
                jpaRepository.save(channelInfoPackage);
            }
        }
    }

    @Override
    public void deleteByPackageId(Long packageId) {
        channelInfoPackageDao.deleteByChannelPackageId(packageId);
    }

    @Override
    @Transactional
    public void deleteChannelInfo(String channels, Long packageId) {
        String[] idArray = channels.split(",|，");
        ChannelPackage channelPackage= channelPackageService.findById(packageId);
        if(channelPackage != null) {
            channelPackage.setIsPublish(0);
            ChannelPackage.putTimeAndUser(channelPackage);
            channelPackageService.save(channelPackage);
            for(String channel:idArray){
                channelInfoPackageDao.deleteByChannelPackageIdAndAndChannelId(packageId,channel);
            }
        }
    }

    @Override
    public ChannelInfoPackage deleteLogic(Long id) {
        return null;
    }

    @Override
    public ChannelInfoPackage merge(ChannelInfoPackage model) {
        return null;
    }
}
